import GameCtrl, { GameRoleCtrl } from "../../../game/GameCtrl";
import BaseWidget from "../../BaseWidget";
import OrientationWidget from "../../OrientationWidget";

const {ccclass, property, menu} = cc._decorator;

@ccclass
@menu("Widget/Game/GameControllerWidget")
export default class GameControllerWidget extends OrientationWidget{
    @property(cc.Node)
    node_handler: cc.Node = null;

    private _canTouch = false;
    private _ctrl: GameRoleCtrl = GameRoleCtrl.NONE;

    onLoad(){
        super.onLoad();
    }

    public init(){
        this.node.opacity = 128;
        this._canTouch = true;
        this._ctrl = GameRoleCtrl.NONE;

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    public stop(){
        this._canTouch = false;
    }

    public getCtrl(){
        return this._ctrl;
    }

    private onTouch(event: cc.Event.EventTouch){
        if(!this._canTouch) return;

        let worldPos = event.getLocation();
        let localPos = this.node.convertToNodeSpaceAR(worldPos);
        let ctrl = GameRoleCtrl.NONE;
        
        if(localPos.len() > 20){
            let angle = localPos.signAngle(cc.Vec2.UP);
            if(angle > -Math.PI * 1 / 4 && angle < Math.PI / 4){
                //up
                ctrl = GameRoleCtrl.UP;
            }else if(angle > Math.PI * 1 / 4 && angle < Math.PI * 3 / 4){
                //right
                ctrl = GameRoleCtrl.RIGHT;
            }else if(angle > Math.PI * 3 / 4 || angle < -Math.PI * 3 / 4){
                //down
                ctrl = GameRoleCtrl.DOWN;
            }else{
                //left
                ctrl = GameRoleCtrl.LEFT;
            }
        }
        // if(ctrl == this._ctrl) return;
        this.node_handler.setPosition(localPos);
        this._ctrl = ctrl;

        this.node.opacity = 255;
    }

    private onTouchEnd(event: cc.Event){
        if(!this._canTouch) return;

        let ctrl = GameRoleCtrl.NONE;
        // if(ctrl == this._ctrl) return;

        this._ctrl = ctrl;
        this.node_handler.setPosition(cc.Vec2.ZERO);
        
        this.node.opacity = 128;
    }

    protected onCanvasSizeChanged(): void {
        let widget = this.getComponent(cc.Widget);
        if(cc.view.getVisibleSize().width > cc.view.getVisibleSize().height){
            //横
            widget.isAlignBottom = false;
            widget.isAlignHorizontalCenter = false;
            widget.isAlignLeft = true;
            widget.isAlignVerticalCenter = true;
            widget.left = 95;
            widget.verticalCenter = 0;
        }else{
            //竖
            widget.isAlignBottom = true;
            widget.isAlignHorizontalCenter = true;
            widget.isAlignLeft = false;
            widget.isAlignVerticalCenter = false;
            widget.bottom = 95;
            widget.horizontalCenter = 0;
        }
        widget.updateAlignment();
    }
}